<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 14:08
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\Organization;

class OrganizationLogic extends BaseLogic
{

    private $OrganizationModel;

    public function __construct()
    {
        parent::__construct();
        $this->OrganizationModel = new Organization();
    }

    public function cateList($tempWhere, $nowPage){
        if ($tempWhere['name']){
            $where[] = ['name', 'like', "%".$tempWhere['name']."%"];
        }
        if (is_numeric($tempWhere['status']) === true){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        $count = Organization::where($where)->count();
        if ($count <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        } else{
            $cateList = Organization::where($where)->page($nowPage, self::$pageNum)->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $cateList, $count);
        }
    }

    /**
     * 分类信息
     */
    public function cateInfo($id){
        $cateInfo = $this->OrganizationModel->get($id);
        if ($cateInfo){
            return ajaxArr(ErrorCode::$SUCCESS, 'success',$cateInfo);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '分类信息为空');
        }
    }

    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                $res = $this->OrganizationModel->allowField(true)->save($data);
                break;
            case 'edit':
                if (!$data['status']){
                    $data['status'] = 0;
                }
                $res = $this->OrganizationModel->allowField(true)->save($data,['id' => $data['id']]);
                break;
            case 'del':
                $cateInfo = Organization::get($data['id']);
                $res = $cateInfo->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

    public function getAllCate(){
        $where[] = ['status', '=', 1];
        $cateList = $this->OrganizationModel->where($where)->select();
        if ($cateList){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $cateList);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }
}